package net.wanho.shop.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import net.wanho.shop.dto.PasswordDTO;
import net.wanho.shop.dto.UserDTO;
import net.wanho.shop.po.User;
import net.wanho.shop.service.UserService;
import net.wanho.shop.vo.AjaxResult;
import net.wanho.shop.vo.PageVo;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.Serializable;


/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 猛虎王
 * @since 2024-12-24
 */
@Api(tags = "02.用户表接口")
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    @ApiOperation("用户表分页查询")
    @GetMapping
    public AjaxResult findPage(@ApiParam("页码") @RequestParam(defaultValue = "1") int pageNum,
                               @ApiParam("页大小") @RequestParam(defaultValue = "5") int pageSize,
                               @ApiParam("查询条件") User user) {
        PageVo pageVo = userService.findPage(pageNum, pageSize, user);
        return AjaxResult.success(pageVo);
    }

    @ApiOperation("根据主键查询用户表")
    @GetMapping("/{id}")
    public AjaxResult findById(@PathVariable Serializable id) {
        return AjaxResult.success(userService.findById(id));
    }

    @ApiOperation("新增用户表")
    @PostMapping
    public AjaxResult add(@RequestBody User user) {
        userService.add(user);
        return AjaxResult.success();
    }

    @ApiOperation("修改用户表")
    @PutMapping
    public AjaxResult modify(@RequestBody User user) {
        userService.modify(user);
        return AjaxResult.success();
    }

    @ApiOperation("根据主键删除用户表")
    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable Serializable id) {
        userService.removeById(id);
        return AjaxResult.success();
    }

    @ApiOperation("修改用户状态")
    @PutMapping("state/{id}/{state}")
    public AjaxResult modifyState(@PathVariable("id") Integer id, @PathVariable("state") Integer state) {
        userService.modifyState(id, state);
        return AjaxResult.success("修改状态成功");
    }

    @ApiOperation("编辑用户信息")
    @PutMapping("{id}")
    public AjaxResult modifyUser(@PathVariable("id") Integer id, @RequestBody User user) {
        userService.modifyUser(id, user);
        return AjaxResult.success("编辑成功");
    }

    @ApiOperation("修改用户角色")
    @PutMapping("role/{id}/{roleId}")
    public AjaxResult modifyRole(@PathVariable("id") Integer id, @PathVariable("roleId") Integer roleId) {
        userService.modifyRole(id, roleId);
        return AjaxResult.success("修改角色成功");
    }

    @ApiOperation("修改用户密码")
    @PutMapping("modifyPwd")
    public AjaxResult modifyPassword(@RequestBody PasswordDTO passwordDTO) {
        userService.modifyPassword(passwordDTO);
        return AjaxResult.success("修改密码成功");
    }

    @ApiOperation("修改用户信息")
    @PutMapping("modifySelf")
    public AjaxResult modifySelf(@RequestBody UserDTO useDTO) {
        userService.modifyUserInfo(useDTO);
        return AjaxResult.success("修改个人信息成功");
    }

    @ApiOperation("修改用户头像")
    @PostMapping("modifyAvatar/{id}")
    public AjaxResult modifyHead(@PathVariable("id") Integer id, MultipartFile file) {
        String url = userService.modifyUserHead(id, file);
        return AjaxResult.success("修改头像成功", url);
    }

}
